﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net.Udp;
using System.Threading;
namespace GodKA
{
    class Program
    {
        static List<string> receive_string = new List<string>();
        static void Main(string[] args)
        {
            Console.Write("Server-Scode-Udp\n================================\n1.save udp data\n2.send udp data\n");
            string getkeyvalue = Console.ReadKey(true).Key.ToString();
            switch (getkeyvalue)
            {
                case "D1":
                    udpdata_save();
                    //return;
                    break;
                case "D2":
                    udpdata_send();
                    break;
            }
        }
        private static void udpdata_save()
        {
            IniOperation ini = new IniOperation(@".\config.ini");
            int port = ini.ReadIniInt("config", "port");
            UDPSocket udp = new UDPSocket(port, 8888, update_arrive);
            udp.StartRecvThreadListener();
            //udp.IP = IP;
            Console.Write("Stop?(Y/N)\n");
            while (Console.ReadKey(true).Key.ToString() == "Y")
            {
                udp.Done = true;
                using (StreamWriter sw = new StreamWriter(@".\" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".txt"))
                {
                    foreach (string str in receive_string)
                    {
                        sw.WriteLine(str);
                    }
                }
                Console.WriteLine("Done");
                //return;
                break;
            }
            return;
        }
        private static void update_arrive(byte[] data)
        {
            string tmpstring = string.Empty;
            foreach (byte b in data)
            {
                tmpstring += b + " ";
            }
            receive_string.Add(tmpstring);
            Console.WriteLine(tmpstring);
        }
        private static void udpdata_send()
        {
            IniOperation ini = new IniOperation(@".\config.ini");
            int port = ini.ReadIniInt("config", "port");
            int fps = ini.ReadIniInt("config", "fps");
            string IP = ini.ReadIniString("config", "IP");
            UDPSocket udp = new UDPSocket(8888, port);
            udp.IP = IP;
            List<Byte[]> SendByte = new List<Byte[]>();
            using (StreamReader sr = new StreamReader(@".\2008-05-27-datalog1.txt"))
            {
                String line;
                while ((line = sr.ReadLine()) != null)
                {
                    try
                    {
                        if (line[0] != '	')
                        {
                            SendByte.Add(stringtoHex(line));
                        }
                    }
                    catch { }
                }
            }
            //while (true)
            do
            {
                foreach (byte[] sendbytes in SendByte)
                {
                    Console.WriteLine(DateTime.Now.ToString());
                    udp.send(sendbytes);
                    Thread.Sleep(1000 / fps);//60fps
                }
                Console.Write("AGAIN? (Y/N)\n");
            } while (Console.ReadKey(true).Key.ToString() == "Y");
            //return;
            //Console.ReadKey();
        }
        static byte[] stringtoHex(string hexValues)
        {
            string[] hexValuesSplit = hexValues.Split(' ');
            List<Byte> ListByte = new List<byte>();
            foreach (String hex in hexValuesSplit)
            {
                if (hex != string.Empty)
                {
                    // Convert the number expressed in base-16 to an integer. 
                    byte value = Convert.ToByte(hex, 16);
                    ListByte.Add(value);
                }
            }
            return ListByte.ToArray();
        }
    }
}
